package dao;

import hbt.HibernateUtil;

import negocio.Pedido;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class PedidoDAO 
{
	private static PedidoDAO instancia = null;
	private static SessionFactory sf = null;
	private Session session = null;
	
	public static PedidoDAO getInstancia()
	{
		if(instancia == null)
		{
			sf = HibernateUtil.getSessionFactory();
			instancia = new PedidoDAO();
		} 
		return instancia;
	}
	
	private Session getSession()
	{
		if(session == null || !session.isOpen()){
			if(sf.isClosed()){
				session = sf.openSession();
			}
			else
				session = sf.getCurrentSession();
		}
		return session;
	}
	
	public void closeSession()
	{
		if (session.isOpen()) session.close();
	}
	
	public void persistPedido(Pedido pedido)
	{
		Session s=getSession();
		s.beginTransaction();
		s.persist(pedido);
		s.flush();
		s.getTransaction().commit();
		closeSession();
	}

	public void delete(Object o) {
		Session s=getSession();
		s.beginTransaction();
		s.delete(o);
		s.flush();
		s.getTransaction().commit();
		closeSession();
	}
}
